import { Button } from "antd"
import axios from "axios"
import { Link } from "react-router-dom"
import { API } from "../../config"
import { isAuth } from "../../helpers/auth"

export default function PayButton({address, cart, totalPrice}) {
    const handlePay = () => {
        axios.post(`${API}/alipay`, {
            totalAmount: totalPrice,
            subject:'',
            body: '',
            products:cart.map(product => ({
                product: product._id,
                count: product.count
            })),
            address,
            userId:isAuth().user._id
        }).then(res => {
            window.localStorage.href = res.data.result
        })
    }
    const showButton = () => {
        return isAuth() ? <Button onClick={handlePay}>提交</Button> : <Button><Link to='/signin'>登录</Link></Button>
    }
    return (
        <div>{showButton()}</div>
    )
}